package com.zzxz.practicaltrainingsys.controller;

import com.zzxz.practicaltrainingsys.entity.College;
import com.zzxz.practicaltrainingsys.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/college")
@CrossOrigin
public class CollegeController {
    @Autowired
    private CollegeService collegeService;

    @GetMapping("/list")
    public List<College> list() {
        return collegeService.getCollege();
    }

    // 新增学院接口
    @PostMapping("/insert")
    public int insertCollege(@RequestBody College college) {
        if (college.getCollege_name() == null || college.getCollege_name().trim().isEmpty() || college.getSchool_id() == null) {
            return -1;
        }
        // 校验同一学校下学院名是否重复（可选）
        List<College> all = collegeService.getCollege();
        for (College c : all) {
            if (c.getSchool_id().equals(college.getSchool_id()) && c.getCollege_name().equals(college.getCollege_name())) {
                return 0;
            }
        }
        boolean success = collegeService.addCollege(college);
        return success ? 1 : -2;
    }

    //更新
    @PostMapping("/update")
    public int updateCollege(@RequestBody College college){
        int result = collegeService.updateCollege(college);
        return result;
    }

    //根据学校id查学院
    @GetMapping("/findcollege")
    public List<College> getCollegeBySchoolId(@RequestParam Integer school_id){
        return collegeService.getCollegeBySchoolId(school_id);
    }
}